package com.rd.student.jike.class2.lihongming.Solutions5;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;


/**
 * @Auther:Heinrich
 * @Date:2023/7/30-07-30-22:23
 * @Description:com.rd.student.jike.class2.lihongming.Solutions5
 * @version:1.0
 */
public class Question6 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入要随机的人数（输入-1退出系统）");
            int order  =   sc.nextInt();
            if(order == -1)break;
            System.out.println(randomName(order));
        }
    }
    public static LinkedList<String> randomName(int nameNum) throws IOException {
        LinkedList<String> nameResult = new LinkedList<>();
        String names = reader("all.txt");
        String[] nameRandomEd = (reader("random.txt").concat(reader("leave.txt"))).split("、");
        LinkedList<String> nameTotal = new LinkedList<>(Arrays.asList(names.split("、")));
        if (nameRandomEd.length > 0) {
            int length = nameTotal.size();
            int i = 0;
            while ( length > i) {
                int temp = 0;
                for (int j = 0; j < nameRandomEd.length; j++) {
                    if (nameTotal.contains(nameRandomEd[j])) {
                        nameTotal.remove(i);
                        temp =1;
                        break;
                    }
                }
                length = nameTotal.size();
                if(temp==0){
                    i++;
                }
            }
        }
        //System.out.println(nameTotal);
        Collections.shuffle(nameTotal);
        //当random.txt剩余人数不足一次随机时放弃本次随机
        if(nameNum > nameTotal.size()){
            //先将储存已随机过同学的all.txt清空
            writer("random.txt",new LinkedList<>(),false);
            //再重新调用randomName方法得出结果
            LinkedList<String> newResult = randomName(nameNum-nameTotal.size());
            for (String name:newResult) {
                nameTotal.add(name);
            }
            return nameTotal;
        }
        for (int i = 0; i < nameNum; i++) {
            nameResult.add(nameTotal.get(i));
        }
        writer("random.txt",nameResult,true);
        return nameResult;
    }
    public static String reader(String path) throws IOException {
        File fileRandom =  new File(path);
        FileInputStream isRandom = new FileInputStream(fileRandom);
        byte[]  pipe = new byte[(int) fileRandom.length()];
        isRandom.read(pipe);
        isRandom.close();
        return new String(pipe);
    }
    public static void writer(String path,LinkedList<String> names,boolean concat) throws IOException {
        File file = new File(path);
        FileOutputStream os = new FileOutputStream(file,concat);
        for (int i = 0; i < names.size(); i++) {
            String name = names.get(i) +  "、";
            byte[] pipe  = name.getBytes();
            os.write(pipe);
        }
        os.close();
    }
}

